﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

using System.Net.Http;
using System.Web.Http.Routing;
using $prjsn$.RestApi;
using Newtonsoft.Json;

using ProjectBase.Core;
using ProjectBase.Core.Service;

namespace $safeprojectname$.Project.RestApi
{
    public static class RestApiExt
    {
        public static JsonTextWriter CreateJsonWriter(this StringWriter writer)
        {
            var jsonWriter = new JsonTextWriter(writer)
            {
                Indentation = 1
            };
            return jsonWriter;
        }

        public static JsonTextReader CreateJsonReader(this string source)
        {
            var reader = new StringReader(source);
            var jsonReader = new JsonTextReader(reader);
            return jsonReader;
        }

        public static JsonSerializer CreateSerializer<TEntity>(this TEntity entity)
            where TEntity : class, IPersistentObjectWithTypedId<int>
        {
            var resolver = Factory.CreateInstance<EntityResolver<TEntity, int>>();
            
            var settings = new JsonSerializerSettings
            {
                ContractResolver = resolver,
            };
            var serializer = JsonSerializer.Create(settings);
            return serializer;
        }

        public static JsonSerializer CreatePOSTSerializer<TEntity>(this TEntity entity)
            where TEntity : class, IPersistentObjectWithTypedId<int>
        {
            var resolver = CreateResolver<TEntity>();
            var settings = new JsonSerializerSettings
            {
                ContractResolver = resolver,
            };
            var serializer = JsonSerializer.Create(settings);
            return serializer;
        }
        public static JsonSerializer CreatePUTSerializer<TEntity>(this TEntity entity, int id)
            where TEntity : class, IPersistentObjectWithTypedId<int>
        {
            var resolver = CreateResolver<TEntity>(id);
            var settings = new JsonSerializerSettings
            {
                ContractResolver = resolver,
            };
            var serializer = JsonSerializer.Create(settings);
            return serializer;
        }

        public static EntityResolver<TEntity, int> CreateResolver<TEntity>(int? id = null)
            where TEntity : class, IPersistentObjectWithTypedId<int>
        {
            var resolver = Factory.CreateInstance<EntityResolver<TEntity, int>>();

            var message = new HttpRequestMessage
            {
                Method = id.HasValue ? HttpMethod.Put : HttpMethod.Post,
            };
            var context = new System.Web.Http.Controllers.HttpControllerContext
            {
                Request = message, 
                RouteData = new HttpRouteData(new HttpRoute(), new HttpRouteValueDictionary
                {
                    {"controller", "service"},
                    {"id", id}
                })
            };

            resolver.EntityConverter.Context = context;

            return resolver;
        }
    }
}
